package cn.tedu.scc.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class uploadController {

    @Value("${dirPath}")
    private String dirPath;
    @RequestMapping("/upload")
    public String upload(MultipartFile picFile) throws IOException {
        String fileName=picFile.getOriginalFilename();
        String suffix=fileName.substring(fileName.indexOf("."));
        fileName= UUID.randomUUID()+suffix;
        File dirFile=new File(dirPath);
        if(!dirFile.exists()){
            dirFile.mkdirs();
        }
        String filePath=dirPath+"/"+fileName;
        picFile.transferTo(new File(filePath));
        return fileName;
    }

    @RequestMapping("/upload/delete")
    public void delete(String name){
        String filePath=dirPath+"/"+name;
        new File(filePath).delete();
    }
}
